package io.github.lingalone.monsterauth.controller;


import io.github.lingalone.monsterauth.auth.AuthException;
import io.github.lingalone.monsterauth.auth.AuthProperties;
import io.github.lingalone.monsterauth.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 *
 * @author <a href="https://github.com/lingalone">lingalone</a>
 * @version 1.0.0
 * @link
 * @since 2018/7/30
 */

@RestController
@RequestMapping("/auth/api/v1/")
public class AuthController {

    @Autowired
    private AuthProperties authProperties;

    @Autowired
    JwtUtil jwtUtil;

    @GetMapping(value = "/getSubject")
    public void getSubject(@RequestParam(name = "token", required = false) String token, HttpServletRequest request) {
        String username = null;
        if(StringUtils.isNotBlank(token)){
            username = jwtUtil.getSubject(token, authProperties.getJwtKey());
        }else{
            username = jwtUtil.getSubject(request,authProperties.getJwtCookieName(), authProperties.getJwtKey());
        }

        if(StringUtils.isBlank(username)){
            throw new AuthException("token 过期请重新登录");
        }
    }
}
